package top.shiyiri.team.service;

import top.shiyiri.team.domain.*;

import static top.shiyiri.team.service.Data.*;
/**
 * @Description 负责将 Data 中的数据封装到 Employee[] 数组中
 */
public class NameListService {

    private Employee[] employees;

    public NameListService() {
        /*
        1.根据项目提供的Data类构建相应大小的employees数组
        2.再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，
          以及相关联的Equipment子类的对象
        3.将对象存于数组中
         */
        employees = new Employee[Data.EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {
            int type = Integer.parseInt(EMPLOYEES[i][0]);

            //获取Employee的四个基本信息
            int id = Integer.parseInt(EMPLOYEES[i][1]);
            String name = EMPLOYEES[i][2];
            int age = Integer.parseInt(EMPLOYEES[i][3]);
            double salary = Double.parseDouble(EMPLOYEES[i][4]);
            Equipment equipment;
            double bonus;
            switch (type) {
                case EMPLOYEE:
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case DESIGNER:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Designer(id, name, age, salary, equipment, bonus);
                    break;
                case ARCHITECT:
                    equipment = createEquipment(i);
                    bonus = Integer.parseInt(EMPLOYEES[i][5]);
                    int stock = Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
                    break;
            }
        }
    }

    /**
     * @Description 获取指定index位置上的设备
     * @param i
     * @return
     */
    private Equipment createEquipment(int i) {

        int type = Integer.parseInt(EQUIPMENTS[i][0]);
        String model = EQUIPMENTS[i][1];
        switch (type) {
            case PC://21
                return new PC(model, EQUIPMENTS[i][2]);
            case NOTEBOOK://22
                double price = Double.parseDouble(EQUIPMENTS[i][2]);
                return new NoteBook(model, price);
            case PRINTER://23
                String name = EQUIPMENTS[i][1];
                return new Printer(name, EQUIPMENTS[i][2]);
        }
        return null;
    }

    /**
     * @Description 获取当前所有员工
     * @return
     */
    public Employee[] getEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException {
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id) {
                return employees[i];
            }
        }
        throw new TeamException("找不到指定的员工");
    }
}
